// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;

namespace svs.Mobile.BeyondScheduler.Core
{
	public class CoreHelper
	{
		#region Public Methods

		public enum PowerState
		{
			On = 0x00010000,
			Off = 0x00020000,
			Unattended = 0x00400000,
			UserIdle = 0x01000000,
			Suspend = 0x00200000,
			BackLightOn = 0x02000000
		}

		public static string GetTotalMemoryUsedAsString()
		{
			long mem = GC.GetTotalMemory(true);
			return mem/1024 + " K";
		}

		public static void WriteLogEntry(string i_entry)
		{
			StreamWriter sr = null;

			try
			{
				sr = File.AppendText(Configuration.LogFilePath);
				sr.WriteLine(string.Format("{0}: {1}", DateTime.Now, i_entry));
				sr.Close();
			}
			catch
			{
			}
			finally
			{
				if (sr != null)
					sr.Close();
			}
		}

		public static int PostWindowsMessage(string windowClass, string windowName, int winMessage, int wParam, string lparam)
		{
			IntPtr hwnd = FindWindow(windowClass, windowName);
			if (hwnd.Equals(IntPtr.Zero))
			{
				return -1;
			}

			Message m = Message.Create(hwnd, winMessage, (IntPtr) wParam, IntPtr.Zero);
			MessageWindow.PostMessage(ref m);
			return (int) m.Result;
		}

		[DllImport("BthUtil.dll")]
		public static extern int BthSetMode(RadioMode dwMode);

		[DllImport("BthUtil.dll")]
		public static extern int BthGetMode(ref RadioMode dwMode);


/*const int POWER_STATE_ON         = 0x00010000;      // on state
const int POWER_STATE_OFF        = 0x00020000;      // no power, full off
const int POWER_STATE_CRITICAL   = 0x00040000;      // critical off
const int POWER_STATE_BOOT       = 0x00080000;      // boot state
const int POWER_STATE_IDLE       = 0x00100000;      // idle state
const int POWER_STATE_SUSPEND    = 0x00200000;      // suspend state
const int POWER_STATE_UNATTENDED = 0x00400000;      // Unattended state.
const int POWER_STATE_RESET      = 0x00800000;      // reset state
const int POWER_STATE_USERIDLE   = 0x01000000;      // user idle state
const int POWER_STATE_BACKLIGHTON= 0x02000000;      // device scree backlight on
const int POWER_STATE_PASSWORD   = 0x10000000;      // This state is password protected.*/

		[DllImport("coredll.dll", SetLastError = true)] 
		private static extern int GetSystemPowerState(string pBuffer, int Length, int pFlags);


		[DllImport("coredll.dll", SetLastError = true)]
		private static extern int SetSystemPowerState(string psState, int StateFlags, int Options);

		public static void SetDevicePowerState(PowerState state)
		{
			SetSystemPowerState(null, (int) state, 0);
		}

		#endregion

		#region Private methods

		[DllImport("coredll.dll", EntryPoint = "FindWindow")]
		private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


		#endregion

		#region Global Enums

		public enum RadioMode
		{
			Off,
			On,
			Discoverable,
		}

		#endregion
	}
}